package com.example.chenduoyun.profect.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author by created chen cloudy 2018/9/11 19:16
 **/

public class ToggleView extends View {
    private Bitmap switchBackgroundBitmap;
    private Paint paint;
    private Bitmap slideButtonBitmap;
    private int newLeft;
    private boolean mSwitchState = false;
    private float currentX;
    private OnSwithchStateUpdateListener onSwitchStateUpdateListener;

    public ToggleView(Context context) {
        super(context);
        init();
    }

    public ToggleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        String namespace="http://schemas.android.com/apk/res-auto";
        int switch_background = attrs.getAttributeResourceValue(namespace, "switch_background", -1);
        setSwitchBackgroundResource(switch_background);
        int switch_button = attrs.getAttributeResourceValue(namespace, "switch_button", -1);
        setSlideButtonResource(switch_button);
        mSwitchState = attrs.getAttributeBooleanValue(namespace, "state", false);
    }

    public ToggleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(switchBackgroundBitmap.getWidth(), switchBackgroundBitmap.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        newLeft = switchBackgroundBitmap.getWidth() - slideButtonBitmap.getWidth();
        canvas.drawBitmap(switchBackgroundBitmap, 0, 0, paint);
        if (isTouchMode) {
            float newNext = currentX - slideButtonBitmap.getWidth() / 2;
            if (newNext < 0) {
                newNext = 0;
            } else if (newNext > newLeft) {
                newNext = newLeft;
            }
            canvas.drawBitmap(slideButtonBitmap, newNext, 0, paint);
        } else {
            if (mSwitchState) {
                canvas.drawBitmap(slideButtonBitmap, newLeft, 0, paint);
            } else {
                canvas.drawBitmap(slideButtonBitmap, 0, 0, paint);
            }
        }
    }

    boolean isTouchMode = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouchMode = true;
                Log.i("cdy", "ACTION_DOWN" + event.getX());
                currentX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("cdy", "ACTION_MOVE" + event.getX());
                currentX = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                isTouchMode = false;
                Log.i("cdy", "ACTION_UP" + event.getX());
                currentX = event.getX();
                float center = switchBackgroundBitmap.getWidth() / 2.0f;
                boolean state = center < currentX;
                if(onSwitchStateUpdateListener!=null&&state!=mSwitchState){
                    onSwitchStateUpdateListener.onStateUpdate(state);
                }
                mSwitchState = state;
                break;
            default:
                break;
        }
        invalidate();
        return true;
    }

    public void setSlideButtonResource(int slide_button) {
        slideButtonBitmap = BitmapFactory.decodeResource(getResources(),
                slide_button);
    }

    public void setSwitchBackgroundResource(int switch_background) {
        switchBackgroundBitmap = BitmapFactory.decodeResource(getResources(),
                switch_background);
    }

    public void setSwitchState(boolean mSwitchState) {
        this.mSwitchState = mSwitchState;
    }

    public void setOnSwitchStateUpdateListener(
            OnSwithchStateUpdateListener onSwitchStateUpdateListener) {
        this.onSwitchStateUpdateListener = onSwitchStateUpdateListener;
    }

    public interface OnSwithchStateUpdateListener {
        void onStateUpdate(boolean state);
    }

}
